package com.xms.ribbonbutton.service;

import com.xms.ribbonbutton.core.RibbonButtonArea;
import com.xms.ribbonbutton.core.SystemRibbonButtonJsLib;
import com.xms.ribbonbutton.entity.RibbonButton;
import com.xms.schema.core.EntityMask;
import com.xms.utils.UUIDUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * DefaultButtonProvider
 * </p>
 *
 * @author migo
 * @since 2021/9/8
 */
@Service
public class DefaultButtonProvider implements IDefaultButtonProvider{

    @Override
    public List<RibbonButton> get(int entityMask) {
        List<RibbonButton> buttons = new ArrayList<>();
        //listhead
        RibbonButton addButton = new RibbonButton();
        addButton.setRibbonbuttonid(UUIDUtil.get("00000000-0000-0000-0000-130000000100"));
        addButton.setCssclass("btn btn-link btn-sm");
        addButton.setDisplayorder(0);
        addButton.setIcon("glyphicon glyphicon-plus-sign");
        addButton.setJsaction("CreateRecord(true)");
        addButton.setJslibrary(SystemRibbonButtonJsLib.ENTITY);
        addButton.setLabel("新增");
        addButton.setShowarea(RibbonButtonArea.ListHead);
        buttons.add(addButton);
        RibbonButton delButton = new RibbonButton();
        delButton.setRibbonbuttonid(UUIDUtil.get("00000000-0000-0000-0000-130000000101"));
        delButton.setCssclass("btn btn-link btn-sm");
        delButton.setDisplayorder(1);
        delButton.setIcon("glyphicon glyphicon-trash");
        delButton.setJsaction("DeleteRecord()");
        delButton.setJslibrary(SystemRibbonButtonJsLib.ENTITY);
        delButton.setLabel("删除");
        delButton.setShowarea(RibbonButtonArea.ListHead);
        buttons.add(delButton);
        RibbonButton exportButton = new RibbonButton();
        exportButton.setRibbonbuttonid(UUIDUtil.get("00000000-0000-0000-0000-130000000102"));
        exportButton.setCssclass("btn btn-link btn-sm");
        exportButton.setDisplayorder(10);
        exportButton.setIcon("glyphicon glyphicon-export");
        exportButton.setJsaction("Xms.Data.Export(Xms.Page.PageContext.QueryId, 0, filters)");
        exportButton.setJslibrary(SystemRibbonButtonJsLib.XMS);
        exportButton.setLabel("导出");
        exportButton.setShowarea(RibbonButtonArea.ListHead);
        buttons.add(exportButton);
        //listrow
        //buttons.add(new RibbonButton() { RibbonButtonId = UUIDUtil.get("00000000-0000-0000-0000-130000000102"), CssClass = "btn btn-link btn-xs", DisplayOrder = 0, Icon = "glyphicon glyphicon-edit", JsAction = "EditRecord(true)", JsLibrary = SystemRibbonButtonJsLib.ENTITY, Label = _loc["edit"], ShowArea = RibbonButtonArea.ListRow });
        //buttons.add(new RibbonButton() { RibbonButtonId = UUIDUtil.get("00000000-0000-0000-0000-130000000103"), CssClass = "btn btn-link btn-xs", DisplayOrder = 1, Icon = "glyphicon glyphicon-trash", JsAction = "DeleteRecord()", JsLibrary = SystemRibbonButtonJsLib.ENTITY, Label = _loc["delete"], ShowArea = RibbonButtonArea.ListRow });
        //form
        RibbonButton formAddButton = new RibbonButton();
        formAddButton.setRibbonbuttonid(UUIDUtil.get("00000000-0000-0000-0000-130000000104"));
        formAddButton.setCssclass("btn btn-link btn-sm");
        formAddButton.setDisplayorder(0);
        formAddButton.setIcon("glyphicon glyphicon-plus-sign");
        formAddButton.setJsaction("CreateRecord()");
        formAddButton.setJslibrary(SystemRibbonButtonJsLib.ENTITY);
        formAddButton.setLabel("新增");
        formAddButton.setShowarea(RibbonButtonArea.Form);
        buttons.add(formAddButton);
        RibbonButton formSaveButton = new RibbonButton();
        formSaveButton.setRibbonbuttonid(UUIDUtil.get("00000000-0000-0000-0000-130000000105"));
        formSaveButton.setCssclass("btn btn-link btn-sm");
        formSaveButton.setDisplayorder(1);
        formSaveButton.setIcon("glyphicon glyphicon-floppy-disk");
        formSaveButton.setJsaction("Save()");
        formSaveButton.setJslibrary(SystemRibbonButtonJsLib.ENTITY);
        formSaveButton.setLabel("保存");
        formSaveButton.setShowarea(RibbonButtonArea.Form);
        buttons.add(formSaveButton);
        RibbonButton formSaveNewButton = new RibbonButton();
        formSaveNewButton.setRibbonbuttonid(UUIDUtil.get("00000000-0000-0000-0000-130000000106"));
        formSaveNewButton.setCssclass("btn btn-link btn-sm");
        formSaveNewButton.setDisplayorder(2);
        formSaveNewButton.setIcon("glyphicon glyphicon-floppy-disk");
        formSaveNewButton.setJsaction("SaveAndNew()");
        formSaveNewButton.setJslibrary(SystemRibbonButtonJsLib.ENTITY);
        formSaveNewButton.setLabel("保存并新增");
        formSaveNewButton.setShowarea(RibbonButtonArea.Form);
        buttons.add(formSaveNewButton);
        if (entityMask == EntityMask.User.value())
        {
            String rules = "{\"FormStateRules\":{\"States\":[\"create\"],\"Enabled\":false,\"Visibled\":false}}";
            //share
            RibbonButton formShareButton = new RibbonButton();
            formShareButton.setRibbonbuttonid(UUIDUtil.get("00000000-0000-0000-0000-130000000107"));
            formShareButton.setCssclass("btn btn-link btn-sm");
            formShareButton.setDisplayorder(6);
            formShareButton.setIcon("glyphicon glyphicon-share-alt");
            formShareButton.setJsaction("Sharing(Xms.Page.PageContext.EntityId, Xms.Page.PageContext.RecordId)");
            formShareButton.setJslibrary(SystemRibbonButtonJsLib.ENTITY);
            formShareButton.setLabel("共享");
            formShareButton.setShowarea(RibbonButtonArea.Form);
            formShareButton.setCommandrules(rules);
            buttons.add(formShareButton);
            //assign
            RibbonButton formAssignButton = new RibbonButton();
            formAssignButton.setRibbonbuttonid(UUIDUtil.get("00000000-0000-0000-0000-130000000108"));
            formAssignButton.setCssclass("btn btn-link btn-sm");
            formAssignButton.setDisplayorder(7);
            formAssignButton.setIcon("glyphicon glyphicon-user");
            formAssignButton.setJsaction("Assigning(Xms.Page.PageContext.EntityId, Xms.Page.PageContext.RecordId)");
            formAssignButton.setJslibrary(SystemRibbonButtonJsLib.ENTITY);
            formAssignButton.setLabel("分派");
            formAssignButton.setShowarea(RibbonButtonArea.Form);
            formAssignButton.setCommandrules(rules);
            buttons.add(formAssignButton);
        }
        String rules2 = "{\"FormStateRules\":{\"States\":[\"create\"],\"Enabled\":false,\"Visibled\":false}}";
        //delete
        RibbonButton formDeleteButton = new RibbonButton();
        formDeleteButton.setRibbonbuttonid(UUIDUtil.get("00000000-0000-0000-0000-130000000109"));
        formDeleteButton.setCssclass("btn btn-link btn-sm");
        formDeleteButton.setDisplayorder(3);
        formDeleteButton.setIcon("glyphicon glyphicon-trash");
        formDeleteButton.setJsaction("DeleteOneRecord()");
        formDeleteButton.setJslibrary(SystemRibbonButtonJsLib.ENTITY);
        formDeleteButton.setLabel("删除");
        formDeleteButton.setShowarea(RibbonButtonArea.Form);
        formDeleteButton.setCommandrules(rules2);
        buttons.add(formDeleteButton);
        //enabled
        String rules3 = "{\"FormStateRules\":{\"States\":[\"create\"],\"Enabled\":false,\"Visibled\":false},\"ValueRules\":{\"Values\":[{\"Field\":\"statecode\",\"Value\":\"1\"}],\"Enabled\":false,\"Visibled\":false}}";
        RibbonButton formEnabledButton = new RibbonButton();
        formEnabledButton.setRibbonbuttonid(UUIDUtil.get("00000000-0000-0000-0000-130000000110"));
        formEnabledButton.setCssclass("btn btn-link btn-sm");
        formEnabledButton.setDisplayorder(4);
        formEnabledButton.setIcon("glyphicon glyphicon-ok-circle");
        formEnabledButton.setJsaction("SetRecordState(1)");
        formEnabledButton.setJslibrary(SystemRibbonButtonJsLib.ENTITY);
        formEnabledButton.setLabel("启用");
        formEnabledButton.setShowarea(RibbonButtonArea.Form);
        formEnabledButton.setCommandrules(rules3);
        buttons.add(formEnabledButton);
        //disabled
        String rules4 = "{\"FormStateRules\":{\"States\":[\"create\"],\"Enabled\":false,\"Visibled\":false},\"ValueRules\":{\"Values\":[{\"Field\":\"statecode\",\"Value\":\"0\"}],\"Enabled\":false,\"Visibled\":false}}";
        RibbonButton formDisabledButton = new RibbonButton();
        formDisabledButton.setRibbonbuttonid(UUIDUtil.get("00000000-0000-0000-0000-130000000111"));
        formDisabledButton.setCssclass("btn btn-link btn-sm");
        formDisabledButton.setDisplayorder(5);
        formDisabledButton.setIcon("glyphicon glyphicon-ban-circle");
        formDisabledButton.setJsaction("SetRecordState(0)");
        formDisabledButton.setJslibrary(SystemRibbonButtonJsLib.ENTITY);
        formDisabledButton.setLabel("禁用");
        formDisabledButton.setShowarea(RibbonButtonArea.Form);
        formDisabledButton.setCommandrules(rules4);
        buttons.add(formDisabledButton);
        buttons.forEach(x ->
                {
                        x.setShowlabel(true);
        x.setStatecode(true);
            });
        return buttons;
    }
}
